package cn.adelyn.blog.blogmanager.dao;

import cn.adelyn.blog.blogmanager.dao.mapper.BlogFileMapper;
import cn.adelyn.blog.blogmanager.pojo.bo.InsertBlogDecryptBO;
import cn.adelyn.blog.blogmanager.pojo.model.BlogFileModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class BlogFileDAOService {

    @Autowired
    BlogFileMapper blogFileMapper;

    public void insertBlogFiles(InsertBlogDecryptBO insertBlogDecryptBO) {
        List<BlogFileModel> blogFileModelList = genBlogFileModelList(insertBlogDecryptBO);
        blogFileMapper.insertBatch(blogFileModelList);
    }
    public List<Long> selectBlogFiles(Long blogId) {
        QueryWrapper<BlogFileModel> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(BlogFileModel::getBlogId, blogId);

        return blogFileMapper.listBlogFiles(queryWrapper);
    }

    public void deleteBlogFiles(Long blogId) {
        QueryWrapper<BlogFileModel> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(BlogFileModel::getBlogId, blogId);

        blogFileMapper.delete(queryWrapper);
    }

    private List<BlogFileModel> genBlogFileModelList(InsertBlogDecryptBO insertBlogDecryptBO) {
        List<BlogFileModel> blogFileModelList = new ArrayList<>();
        insertBlogDecryptBO.getFileIdList().stream().forEach(fileId -> {
            BlogFileModel blogFileModel = new BlogFileModel();
            blogFileModel.setBlogId(insertBlogDecryptBO.getBlogId());
            blogFileModel.setFileId(fileId);
            blogFileModelList.add(blogFileModel);
        });
        return blogFileModelList;
    }
}
